<?php

class Model_Case extends PhalApi_Model_NotORM {

    public function  lists($where, $offset = 0, $limit = 10, $orderby = "name desc") {
        return $this->getORM()
            ->select("id,name,organize,sort,url,icon_url as iconurl,isshow as isindex,ispublic,online_date as onlinedate,ctime")
            ->where($where)
            ->limit($limit, $offset)
            ->order($orderby)
            ->fetchAll();
    }

    public function  count($where, $offset = 0, $limit = 10, $orderby = "name desc") {
        return $this->getORM()
            ->where($where)
            ->limit($limit, $offset)
            ->order($orderby)
            ->count("id");
    }

    public function organizes() {
        $resultDB = $this->getORM()
            ->select("organize")
            ->group("organize")
            ->fetchAll();
        foreach ($resultDB as $r) {
            $result[] = $r["organize"];
        }
        return $result;
    }

    public function delete($id) {
        return $this->getORM()
            ->where(["id" => $id])
            ->delete();
    }
    public function hasid($id) {
        return $this->getORM()
            ->where(["id" => $id])
            ->count("id");
    }
    public function getById($id) {
        return $this->getORM()
            ->select("id,name,organize,sort,url,icon_url as iconurl,isshow as isindex,ispublic,online_date as onlinedate,ctime")
            ->where(["id" => $id])
            ->fetch();
    }

    public function isadded($url) {
        return $this->getORM()
            ->where(["url" => $url])
            ->count("id");
    }

    public function add($data) {
        return $this->getORM()
            ->insert($data);

    }
    public function edit($data,$id) {
        return $this->getORM()
            ->where('id', $id)
            ->update($data);
    }

    protected function getTableName($id) {
        return 'sp_cases';
    }
}
